import request from './request'

export interface LoginParams {
  username: string
  password: string
}

export interface LoginResponse {
  access_token: string
  refresh_token: string
  token_type: string
  expires_in: number
  user?: {
    id: string
    username: string
    email: string
    is_admin?: boolean
  }
}

export const login = (params: LoginParams) =>
  request.post<LoginResponse>('/auth/login', params)

export const refreshToken = (refreshToken: string) =>
  request.post<LoginResponse>('/auth/refresh', {
    refresh_token: refreshToken,
  })

export const oauth2Login = (provider: string) => {
  const baseURL = import.meta.env.VITE_API_BASE_URL || (import.meta.env.DEV ? '' : 'http://localhost:3001')
  const url = baseURL ? `${baseURL}/auth/oauth2/${provider}` : `/auth/oauth2/${provider}`
  window.location.href = url
}


